package cn.tedu.charging.order.mqtt;


import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Mqtt消息生产者
 * 订单服务给EMQX 发送消息
 */

@Slf4j
@Component
public class MqttProducer {

    @Autowired
    private MqttClient mqttClient;

    public Boolean sendMessage(String topic, String message) {
        Boolean result = false;
        try {
            mqttClient.publish(topic, message.getBytes(), 0, false);
            result = true;
        } catch (Exception e) {
            log.error("订单服务向EMQX发送消息失败", e);
        }
        return result;
    }
}
